﻿Imports FinanceiroBLL
Imports FinanceiroDAL
Public Class TransfereFRM
    Dim objcp As New ContasPagarBLL
    Dim objItPagar As New ItensCtasPagarBLL
    Dim objitrecBLL As New itensCtasReceberBLL
    Dim codigo As Integer
    Dim valentrada As Decimal = 0
    Dim valsaida As Decimal = 0
    Private Sub BFechar_Click(sender As System.Object, e As System.EventArgs) Handles BFechar.Click
        Me.Close()
        limpar()
    End Sub
#Region "LOAD"
    Private Sub TransfereFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        limpar()
        objconexao.carregaCombo(CBCxOrigem, "Financeiro.Ofi_Contas", "con_codigo", "con_descricao", "sta_codigo=1", "")
        objconexao.carregaCombo(CBCxDestino, "Financeiro.Ofi_Contas", "con_codigo", "con_descricao", "sta_codigo=1", "")
        CBCxDestino.SelectedValue = -1
        CBCxOrigem.SelectedValue = -1
    End Sub
#End Region
#Region "limpar"
    Sub limpar()
        CBCxDestino.SelectedValue = -1
        CBCxOrigem.SelectedValue = -1
        TXTValorEntrada.Text = ""
        TXTValorSaida.Text = ""
    End Sub
#End Region
    Private Sub BTNSalvar_Click(sender As System.Object, e As System.EventArgs) Handles BTNSalvar.Click
        Dim forn As String = "Débito ref.t  ransf.p/ outra conta"
        Decimal.TryParse(TXTValorSaida.Text.Replace("R$", ""), valsaida)
        Decimal.TryParse(TXTValorEntrada.Text.Replace("R$", ""), valentrada)
        Try
            If CBCxDestino.SelectedValue <> CBCxOrigem.SelectedValue Then
                If CBCxOrigem.SelectedValue <> 0 Then 'testa cx origem
                    If CBCxDestino.SelectedValue <> 0 Then 'testa cx destino
                        If valsaida <> 0 And TXTValorSaida.Text <> "" And TXTValorSaida.Text <> "0,00" Then
                            If valentrada <> 0 And TXTValorEntrada.Text <> "" And TXTValorEntrada.Text <> "0,00" Then
                                objcp = New ContasPagarBLL(codigo, 0, 0, Date.Parse(DTPLancamento.Text), TXTObsorigem.Text, forn) 'sai dinheiro da conta _pagar
                                '   Dim objItPagar As New ItensCtasPagarBLL()
                                objItPagar = New ItensCtasPagarBLL
                                objItPagar.icp_datapagamento = DTPLancamento.Value
                                objItPagar.icp_valor = valsaida
                                objItPagar.pag_codigo = 9 'tranferencia
                                objItPagar.icp_datavencimento = DTPLancamento.Value
                                objItPagar.icp_valorpago = valsaida 'campo total + jam -desc
                                objItPagar.icp_desconto = 0
                                objItPagar.icp_jam = 0
                                objItPagar.icp_parcela = 1 'num de parcela
                                objItPagar.con_codigo = CBCxOrigem.SelectedValue
                                objItPagar.icp_boletocheque = TXTObsorigem.Text
                                objItPagar.sta_codigo = 6
                                objcp.lCtasPagar.Add(objItPagar)
                                objcp.Inserir()
                                If CBCxDestino.SelectedValue <> 0 Then 'Se cx destino nao estiver selecionado
                                    Dim obs As String = "Crédito ref.transferência"
                                    Dim objCre As New ContasReceberBLL
                                    objCre = New ContasReceberBLL(codigo, 0, 0, Date.Parse(DTPLancamento.Text), 0, valentrada) 'Entra Dinheiro -Receber
                                    ' Dim objitrecBLL As New itensCtasReceberBLL
                                    objitrecBLL = New itensCtasReceberBLL
                                    objitrecBLL.icrdtrecebimento = DTPLancamento.Value 'ou txt
                                    objitrecBLL.icrvalor = valentrada
                                    objitrecBLL.pagcodigo = 9 'tranferencia
                                    objitrecBLL.icrdtvencimento = DTPLancamento.Value
                                    objitrecBLL.icrvalrecebido = valentrada 'campo total + jam -desc
                                    objitrecBLL.icrdesconto = 0
                                    objitrecBLL.icrjam = 0
                                    objitrecBLL.icrparcela = 1 'num de parcela
                                    objitrecBLL.icrobs = obs
                                    objitrecBLL.concodigo = CBCxDestino.SelectedValue
                                    objitrecBLL.icrbolcheque = TXTObsdestino.Text
                                    objitrecBLL.stacodigo = 6
                                    objCre.lCtasreceber.Add(objitrecBLL)
                                    objCre.Inserir()
                                    MessageBox.Show("Operação realizada com sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                                    limpar()
                                    Me.Close()
                                End If
                            Else
                                MsgBox("Preencha corretamente o campo selecionado!", MsgBoxStyle.Information)
                                TXTValorEntrada.SelectAll()
                            End If
                        Else
                            MsgBox("Preencha corretamente o campo selecionado!", MsgBoxStyle.Information)
                            TXTValorSaida.SelectAll()
                        End If
                    Else
                        MsgBox("Caixa de destino não selecionado!", MsgBoxStyle.Information)
                        CBCxOrigem.SelectAll()
                    End If
                Else
                    MsgBox("Caixa de origem não selecionado!", MsgBoxStyle.Information)
                    CBCxOrigem.SelectAll()
                End If
            Else
                MsgBox("Seleção de contas Origem e Destino devem ser diferentes!", MsgBoxStyle.Information)
                CBCxOrigem.SelectAll()
            End If
        Catch ex As Exception
        Finally
            'objcp = Nothing
            'objItPagar = Nothing
            'objitrecBLL = Nothing
        End Try
    End Sub

    Private Sub TXTValorSaida_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTValorSaida.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
        End If
    End Sub

    Private Sub TransfereFRM_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If e.KeyChar = Convert.ToChar(13) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub
#Region "ValidaCampos"
    Sub validacampos()
        Try
            If (TXTValorEntrada.Text <> "") Then
                TXTValorEntrada.Text = FormatCurrency(TXTValorEntrada.Text)
            Else : TXTValorEntrada.Text = "0,00"
            End If
            If (TXTValorSaida.Text <> "") Then
                TXTValorSaida.Text = FormatCurrency(TXTValorSaida.Text)
            Else : TXTValorSaida.Text = "0,00"
            End If
            TXTValorEntrada.Text = TXTValorSaida.Text
            TXTValorEntrada.Text = FormatCurrency(TXTValorEntrada.Text.Replace("R$", ""))
            TXTValorSaida.Text = FormatCurrency(TXTValorSaida.Text.Replace("R$", ""))
        Catch ex As Exception
            '    MsgBox("Formato incorreto nos campos!")
        End Try
    End Sub
#End Region
    Private Sub TXTValorSaida_Leave(sender As System.Object, e As System.EventArgs) Handles TXTValorSaida.Leave
        validacampos()
    End Sub
End Class